﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using NUnit.Framework;
using DBSourceToolsLib;
using DBSourceToolsLib.SysObjects;
using DBConnectionLib;

namespace TestDBSourceToolsLibNUnit
{
    [TestFixture]
    [Ignore]
    public class TestSysObjects
    {
        private const string strControlPath = BaseDirectoryHelper.Unused + "\\control";
        private const string strOutputPath = BaseDirectoryHelper.Unused + "\\output";

        [TestFixtureSetUp]
        public void Init()
        {
            // create TestTable from sql script
            Database oDatabase = new Database("PatternsDB", ".", "PatternsDB", strControlPath);
            oDatabase.RunScript(strControlPath + "\\CreateTableViewProcFn_Cleanup.sql");
            oDatabase.RunScript(strControlPath + "\\CreateTableViewProcFn.sql");
            // remove output directoryies
            FileHelper oFileHelper = new FileHelper();
            oFileHelper.DeleteFilesInDir(strOutputPath);
        }
        [TestFixtureTearDown]
        public void Cleanup()
        {
            Database oDatabase = new Database("PatternsDB", ".", "PatternsDB", strControlPath);
            oDatabase.RunScript(strControlPath + "\\CreateTableViewProcFn_Cleanup.sql");
        }
        [Test]
        public void TestEmptySysObjects()
        {
            //Database oDatabase = new Database("PatternsDB", ".", "PatternsDB", strControlPath);
            //DBSourceToolsLib.SysObjects.SysObjects sysObjects = new DBSourceToolsLib.SysObjects.SysObjects(oDatabase
            //    , oDatabase.BaseDirectory);
            ////SysObjects oSysObjects = new SysObjects(UserSettings.Instance().BaseDirectory);
            //Assert.AreEqual(sysObjects.SysObjectsList.Count, 0);
        }
        [Test]
        public void TestSysObjectCounts()
        {
            //Database oDatabase = new Database("PatternsDB", ".", "PatternsDB", strControlPath);
            //DBSourceToolsLib.SysObjects.SysObjects sysObjects = new DBSourceToolsLib.SysObjects.SysObjects(oDatabase
            //    , oDatabase.BaseDirectory);
            //sysObjects.LoadFromDb();
            //Assert.Greater(sysObjects.SysObjectsList.Count, 0);
        }


        [Test]
        public void TestFileNamesAndEnumerators()
        {
            //Database oDatabase = new Database("AdventureWorks", ".", "AdventureWorks", strControlPath);
            //DBSourceToolsLib.SysObjects.SysObjects sysObjects = new DBSourceToolsLib.SysObjects.SysObjects(oDatabase
            //    , oDatabase.BaseDirectory);
            //sysObjects.ProgressEventPublisher += this.EventReceived;
            //sysObjects.LoadFromDisk();

            //Assert.Greater(sysObjects.Count(SysObjectType.Table), 0, "Could not find any tables");
            //foreach (SOTable table in sysObjects.Tables)
            //{
            //    string strFileName = strControlPath + "\\" + oDatabase.DatabaseName + "\\Tables\\" + table.Name + ".sql";
            //    Assert.AreEqual(strFileName, table.FileName);
            //}
            //Assert.Greater(sysObjects.Count(SysObjectType.View), 0, "Could not find any Views");
            //foreach (SOView view in sysObjects.Views)
            //{
            //    string strFileName = strControlPath + "\\" + oDatabase.DatabaseName + "\\Views\\" + view.Name + ".sql";
            //    Assert.AreEqual(strFileName, view.FileName);
            //}

            //Assert.Greater(sysObjects.Count(SysObjectType.Function), 0,"Could not find any Functions");
            //foreach (SOFunction function in sysObjects.Functions)
            //{
            //    string strFileName = strControlPath + "\\" + oDatabase.DatabaseName + "\\Functions\\" + function.Name + ".sql";
            //    Assert.AreEqual(strFileName, function.FileName);
            //}

            //Assert.Greater(sysObjects.Count(SysObjectType.Proc), 0,"Could not find any Procs");
            //foreach (SOProc proc in sysObjects.Procs)
            //{
            //    string strFileName = strControlPath + "\\" + oDatabase.DatabaseName + "\\Procs\\" + proc.Name + ".sql";
            //    Assert.AreEqual(strFileName, proc.FileName);
            //}
            //Assert.Greater(sysObjects.Count(SysObjectType.Querie), 0, "Could not find any Queries");
            //foreach (SOQuery query in sysObjects.Queries)
            //{
            //    string strFileName = strControlPath + "\\" + oDatabase.DatabaseName + "\\Queries\\" + query.Name + ".sql";
            //    Assert.AreEqual(strFileName, query.FileName);
            //}
            //Assert.Greater(sysObjects.Count(SysObjectType.DataType), 0, "Could not find any DataTypes");
            //foreach (SODataType oDataType in sysObjects.DataTypes)
            //{
            //    string strFileName = strControlPath + "\\" + oDatabase.DatabaseName + "\\DataTypes\\" + oDataType.Name + ".sql";
            //    Assert.AreEqual(strFileName, oDataType.FileName);
            //}
            //Assert.Greater(sysObjects.Count(SysObjectType.Schema), 0, "Could not find any Schemas");
            //foreach (SOSchema oDataType in sysObjects.Schemas)
            //{
            //    string strFileName = strControlPath + "\\" + oDatabase.DatabaseName + "\\Schemas\\" + oDataType.Name + ".sql";
            //    Assert.AreEqual(strFileName, oDataType.FileName);
            //}
            //Assert.Greater(sysObjects.Count(SysObjectType.XmlSchema), 0, "Could not find any XmlSchemas");
            //foreach (SOXmlSchema oDataType in sysObjects.XmlSchemas)
            //{
            //    string strFileName = strControlPath + "\\" + oDatabase.DatabaseName + "\\XmlSchemas\\" + oDataType.Name + ".sql";
            //    Assert.AreEqual(strFileName, oDataType.FileName);
            //}


        }

        [Test]
        public void TestWriteFiles()
        {
            // remove output directoryies
            //FileHelper oFileHelper = new FileHelper();
            //oFileHelper.DeleteFilesInDir(strOutputPath);
            //Database oDatabase = new Database("PatternsDB", ".", "PatternsDB", strOutputPath);
            //DBSourceToolsLib.SysObjects.SysObjects sysObjects = new DBSourceToolsLib.SysObjects.SysObjects(oDatabase
            //    , oDatabase.BaseDirectory);
            ////UserSettings.Instance().BaseDirectory = strPath + "\\output\\";
            //sysObjects.ProgressEventPublisher += this.EventReceived;
            //sysObjects.LoadFromDb();
            //sysObjects.WriteFiles(true,true);

            //foreach (SysObject sysObject in sysObjects.SysObjectsList.Values)
            //{
            //    string strFilePath = strControlPath + "\\PatternsDB\\" + sysObject.Type.ToString() + "s\\" + sysObject.Name + ".sql";
            //    DBSourceToolsLib.FileComparer.FileComparer oFileComparer
            //        = new DBSourceToolsLib.FileComparer.FileComparer(strFilePath, sysObject.FileName);
            //    Assert.IsTrue(oFileComparer.AreFilesEqual(), "invalid " + sysObject.FileName);
            //}
        }

        [Test]
        public void TestWriteDropFiles()
        {
//            Database oDatabase = new Database("PatternsDB", ".", "PatternsDB", strOutputPath);
////            UserSettings.Instance().BaseDirectory = strPath + "output";
////            SysObjects oSysObjects = new SysObjects(strPath);
//            DBSourceToolsLib.SysObjects.SysObjects sysObjects = new DBSourceToolsLib.SysObjects.SysObjects(oDatabase
//                , oDatabase.BaseDirectory);
//            sysObjects.ProgressEventPublisher += this.EventReceived;
//            sysObjects.LoadFromDb();
//            sysObjects.LoadDependencyTreeFromDb(null);
//            sysObjects.WriteCreateScript(oDatabase.Name);

//            string strOutputFile = strOutputPath + "\\PatternsDB\\Run_Create_Scripts.bat";
//            string strControlFile = strControlPath + "\\PatternsDB\\Run_Create_Scripts.bat";
            
//            DBSourceToolsLib.FileComparer.FileComparer oFileComparer
//                = new DBSourceToolsLib.FileComparer.FileComparer(strOutputFile, strControlFile);
//            Assert.IsTrue(oFileComparer.AreFilesEqual(), "invalid " + strOutputFile);
        }

        [Test]
        public void TestLoadDatabases()
        {
            UserSettings.Instance().ProjectBaseDirectory = strControlPath + "\\test_databases\\";
            //UserSettings.Instance().LoadDatabasesEx();

            Database oDb = UserSettings.Instance().getSourceDatabase("local");
            Assert.IsNotNull(oDb, " could not load databases from Databases.xml");
            Assert.IsTrue(oDb.DataSourceName.CompareTo(".") == 0, " Datasource name should be .");
            Assert.IsTrue(oDb.DatabaseName.CompareTo("PatternsDB") == 0, " Database name should be PatternsDB");
        }

        private int m_EventCount = 0;
        //[Test]
        //public void TestLoadDelegate()
        //{
        //    Database oDatabase = new Database("AdventureWorks", ".", "AdventureWorks");
        //    UserSettings.Instance().BaseDirectory = strPath + "\\adventure_output\\";
        //    SysObjects oSysObjects = new SysObjects(strPath);
        //    oSysObjects.ProgressEventPublisher += this.EventReceived;

        //    oSysObjects.Load(oDatabase);
        //    oSysObjects.WriteFiles();
        //    Assert.IsTrue(m_EventCount > 0, "No event messages received");
        //}

        private void EventReceived(object source, ProgressEventArgs args)
        {
            this.m_EventCount++;
            System.Console.WriteLine(args.Percent + " " + args.Message);
        }

        [Test]
        public void TestLoadFromDisk()
        {
            //Database oDatabase = new Database("AdventureWorks", ".", "AdventureWorks", strControlPath);
            //DBSourceToolsLib.SysObjects.SysObjects sysObjects = new DBSourceToolsLib.SysObjects.SysObjects(oDatabase
            //    , oDatabase.BaseDirectory);
            //sysObjects.ProgressEventPublisher += this.EventReceived;

            //sysObjects.LoadFromDisk();

            //Assert.IsTrue(sysObjects.Count(SysObjectType.Table)> 0, "No table loaded from disk");
            //Assert.IsTrue(sysObjects.Count(SysObjectType.Proc) > 0, "No procs loaded from disk");
            //Assert.IsTrue(sysObjects.Count(SysObjectType.View) > 0, "No views  loaded from disk");
            //Assert.IsTrue(sysObjects.Count(SysObjectType.Function) > 0, "No functions loaded from disk");
            //Assert.IsTrue(sysObjects.Count(SysObjectType.DataType) > 0, "No DataTypes loaded from disk");
            //Assert.IsTrue(sysObjects.Count(SysObjectType.Schema) > 0, "No Schemas loaded from disk");
        }

        [Test]
        public void TestRenameSysObject()
        {
            //Database oDatabase = new Database("PatternsDB", ".", "PatternsDB", strControlPath);
            //DBSourceToolsLib.SysObjects.SysObjects sysObjects = new DBSourceToolsLib.SysObjects.SysObjects(oDatabase
            //    , oDatabase.BaseDirectory);
            //sysObjects.LoadFromDisk();
            //SysObject m_SysObject = sysObjects.getSysObject("Queries", "Query1");
            //Assert.IsNotNull(m_SysObject, " Could not find sys object Queryies.Query1");
            //sysObjects.RenameSysObject("Queries", "Query1", "Query2");

            //SysObject renamed_SysObject = sysObjects.getSysObject("Queries", "Query2");
            //Assert.IsNotNull(renamed_SysObject, " Could not find renamed sys object Queryies.Query2");
            //Assert.IsTrue(renamed_SysObject.QualifiedName.CompareTo("Querie.Query2") == 0,
            //    "Qualified Name incorrect after rename");
            //Assert.IsTrue(m_SysObject.Text.CompareTo(renamed_SysObject.Text) == 0,
            //    "Text not copied during rename");

        }

        [Test]
        public void TestInvalidRename()
        {
            //try
            //{
            //    Database oDatabase = new Database("PatternsDB", ".", "PatternsDB", strControlPath);
            //    DBSourceToolsLib.SysObjects.SysObjects sysObjects = new DBSourceToolsLib.SysObjects.SysObjects(oDatabase
            //    , oDatabase.BaseDirectory);
            //    sysObjects.LoadFromDisk();
            //    SysObject m_SysObject = sysObjects.getSysObject("Queries", "Query1");
            //    Assert.IsNotNull(m_SysObject, " Could not find sys object Queryies.Query1");
            //    sysObjects.RenameSysObject("Queries", "Query1", "Query1");
            //    Assert.Fail("Rename function should throw an exception");
            //}
            //catch (Exception ex)
            //{

            //}

        }

        [Test]
        public void TestFlagChangedOnText()
        {
            //Database oDatabase = new Database("PatternsDB", ".", "PatternsDB", strControlPath);
            //DBSourceToolsLib.SysObjects.SysObjects sysObjects = new DBSourceToolsLib.SysObjects.SysObjects(oDatabase
            //    , oDatabase.BaseDirectory);
            //sysObjects.LoadFromDisk();
            //SysObject m_SysObject = sysObjects.getSysObject("Queries", "Query1");
            //m_SysObject.Text = "new text";
            //Assert.IsTrue(m_SysObject.IsChanged == true, " IsChanged flag not set on change of text");

        }

        [Test]
        public void TestFlagChangedOnRename()
        {
            //Database oDatabase = new Database("PatternsDB", ".", "PatternsDB", strControlPath);
            //DBSourceToolsLib.SysObjects.SysObjects sysObjects = new DBSourceToolsLib.SysObjects.SysObjects(oDatabase
            //    , oDatabase.BaseDirectory);
            //sysObjects.LoadFromDisk();
            //sysObjects.RenameSysObject("Queries", "Query1", "Query2");
            //SysObject m_SysObject = sysObjects.getSysObject("Queries", "Query2");
            //Assert.IsTrue(m_SysObject.IsChanged == true, " IsChanged flag not set on a rename");

        }

        [Test]
        public void TestFlagNotChangedOnLoadFromDisk()
        {
            //Database oDatabase = new Database("PatternsDB", ".", "PatternsDB", strControlPath);
            //DBSourceToolsLib.SysObjects.SysObjects sysObjects = new DBSourceToolsLib.SysObjects.SysObjects(oDatabase
            //    , oDatabase.BaseDirectory);
            //sysObjects.LoadFromDisk();
            //foreach (SysObject oSysObject in sysObjects.SysObjectsList.Values)
            //{
            //    Assert.IsTrue(oSysObject.IsChanged == false, " IsChanged flag should not be set on LoadFromDisk");
            //}

        }
        [Test]
        public void TestFlagNotChangedOnLoadFromDb()
        {
            //Database oDatabase = new Database("PatternsDB", ".", "PatternsDB", strControlPath);
            //DBSourceToolsLib.SysObjects.SysObjects sysObjects = new DBSourceToolsLib.SysObjects.SysObjects(oDatabase
            //    , oDatabase.BaseDirectory);
            //sysObjects.LoadFromDb();
            //foreach (SysObject oSysObject in sysObjects.SysObjectsList.Values)
            //{
            //    Assert.IsTrue(oSysObject.IsChanged == false, " IsChanged flag should not be set on LoadFromDatabase");
            //}

        }

        [Test]
        public void TestLoadDependencyTreeFromDb()
        {
            //Database oDatabase = new Database("AdventureWorks", ".", "AdventureWorks", strControlPath);
            //DBSourceToolsLib.SysObjects.SysObjects sysObjects = new DBSourceToolsLib.SysObjects.SysObjects(oDatabase
            //    , oDatabase.BaseDirectory);
            //sysObjects.LoadFromDisk();
            //sysObjects.LoadDependencyTreeFromDb(null);
            //Assert.IsNotNull(sysObjects.SODependencyTree, "Dependancy Tree in SysObject is null");

            //string strDepTreeFile = strControlPath + "\\AdventureWorks\\Dependencies\\Tree.txt";
            //string strDepTreeText = "";

            //using (System.IO.StreamReader oStreamReader = new System.IO.StreamReader(strDepTreeFile))
            //{
            //    strDepTreeText = oStreamReader.ReadToEnd();
            //}
            //System.Console.WriteLine(sysObjects.SODependencyTree.Text);
            //Assert.IsTrue(strDepTreeText.CompareTo(sysObjects.SODependencyTree.Text) == 0, "Dependancy tree Text invalid ");
        }

        [Test]
        public void TestLoadDependencyTreeFromDisk()
        {
            //Database oDatabase = new Database("AdventureWorks", ".", "AdventureWorks", strControlPath);
            //DBSourceToolsLib.SysObjects.SysObjects sysObjects = new DBSourceToolsLib.SysObjects.SysObjects(oDatabase
            //    , oDatabase.BaseDirectory);
            //sysObjects.LoadFromDisk();
            //sysObjects.LoadDependencyTreeFromDb(null);

            //Database oDatabase_2 = new Database("AdventureWorks", ".", "AdventureWorks", strControlPath);
            //DBSourceToolsLib.SysObjects.SysObjects sysObjects2 = new DBSourceToolsLib.SysObjects.SysObjects(oDatabase
            //    , oDatabase.BaseDirectory);
            //sysObjects2.LoadFromDisk();
            //sysObjects2.LoadDependencyTreeFromDisk();

            //Assert.IsTrue(sysObjects.SODependencyTree.Text.CompareTo(sysObjects2.SODependencyTree.Text) == 0, "Dependancy tree Text invalid ");
        }

        [Test]
        public void TestLoadDeploymentTargets()
        {
            //Database oDatabase = new Database("AdventureWorks", ".", "AdventureWorks", strControlPath);
            //DBSourceToolsLib.SysObjects.SysObjects sysObjects = new DBSourceToolsLib.SysObjects.SysObjects(oDatabase
            //    , oDatabase.BaseDirectory);
            ////sysObjects.LoadFromDisk();
            ////sysObjects.LoadDependencyTreeFromDb();
            ////sysObjects.LoadDeploymentTargets();

            //Assert.Greater(sysObjects.DeploymentTargets.Count, 0,
            //    "SysObjects should load more than 0 deployment targets");
        }

        [Test]
        public void TestWriteDeploymentTargets()
        {
            //Database oDatabase = new Database("PatternsDB", ".", "PatternsDB", strOutputPath);
            //DBSourceToolsLib.SysObjects.SysObjects sysObjects = new DBSourceToolsLib.SysObjects.SysObjects(oDatabase
            //    , oDatabase.BaseDirectory);
            //sysObjects.LoadFromDb();
            //sysObjects.LoadDependencyTreeFromDb(null);
            //sysObjects.ClearDeploymentTargets();
            //sysObjects.AddDeploymentTarget(new Database("PatternsDBCopy", ".", "PatternsDBCopy", strOutputPath));
            //Assert.Greater(sysObjects.DeploymentTargets.Count, 0,
            //    "SysObjects should load more than 0 deployment targets");

            //sysObjects.WriteDeploymentTargets(oDatabase.Name);

            //// compare files in directory
            //string sourceDir = strControlPath + "\\PatternsDB\\DeploymentTargets";
            //string targetDir = strOutputPath + "\\PatternsDB\\DeploymentTargets";

            //FileHelper oFileHelper = new FileHelper();
            //oFileHelper.CompareDirectories(sourceDir, targetDir);
        }


    }
}
